<?

class CreateDir extends Button  {

    function render($sel) {
        return $this->renderDialogButton('createdir','New dir','Create new directory');
    }

    function renderDialog($sel) {
        return '<form method="POST" action="'.Config::getLink($_GET['path'],'','action=createdir').'"><b>&nbsp;New name:</b> <input type="text" name="dirname"><input type="submit" value="Create" id="dirname"  onClick=" if (document.getElementById(\'dirname\').text.search(\'[^a-zA-Z0-9+_]\') != -1) { alert(\'Wrong name!\'); return false; }"></form>';
    }

    function action($sel) {
        $location = $_SERVER['REQUEST_URI'];
        $location = str_replace('action=createdir','',$location);
        if (!isset($_POST['dirname']) || ($_POST['dirname'] == '')) {
            $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create directory with empty name');
        } elseif (preg_match('[^a-zA-Z0-9_]',$_POST['dirname'])) {
            $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create directory - bad name');
        } else {
            $dir = $sel->getCurrentDirName().$_POST['dirname'];
            if (!file_exists($dir)) {
                @mkdir($dir);
                $location .= Config::getMessage(MESSAGE_INFO,'Directory "<b>'.$_POST['dirname'].'</b>" was created successfully');
            } else {
                $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create directory - name "<b>'.$_POST['dirname'].'</b>" already exists!');
            }
        }
        header('Location: '.$location);
        exit;
    }

}

?>